package com.shaobufan.museum.controllar;

import com.shaobufan.museum.common.BaseResponse;
import com.shaobufan.museum.common.ErrorCode;
import com.shaobufan.museum.common.ResultUtils;
import com.shaobufan.museum.pojo.request.staff.StaffChangeRequest;
import com.shaobufan.museum.pojo.request.staff.StaffLoginRequest;
import com.shaobufan.museum.pojo.request.staff.StaffRegisterRequest;
import com.shaobufan.museum.pojo.vo.StaffVo;
import com.shaobufan.museum.service.StaffService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.shaobufan.museum.contant.StaffContent.*;

/**
 * 员工请求
 */
@RestController
@RequestMapping("/staff")
public class StaffController {
    @Resource
    StaffService staffService;

    @ApiOperation("员工登录")
    @PostMapping("/login")
    public BaseResponse<StaffVo> staffLogin(@RequestBody StaffLoginRequest staffLoginRequest, HttpServletRequest request) {
        String staffAccount = staffLoginRequest.getStaffAccount();
        String staffPassword = staffLoginRequest.getStaffPassword();
        StaffVo staffVo = staffService.staffLogin(staffAccount, staffPassword, request);
        if (staffVo == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, STAFF_LOGIN_NO_ACCOUNT, STAFF_LOGIN_FAIL);
        }
        return ResultUtils.success(staffVo, STAFF_LOGIN_SUCCESS);
    }

    @ApiOperation("员工注册")
    @PostMapping("/register")
    public BaseResponse<Integer> staffRegister(@RequestBody StaffRegisterRequest staffRegisterRequest) {
        String staffAccount = staffRegisterRequest.getStaffAccount();
        String staffPassword = staffRegisterRequest.getStaffPassword();
        String checkPassword = staffRegisterRequest.getCheckPassword();

        if (!staffAccount.equals(checkPassword)) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, STAFF_CHECK_FAIL, STAFF_REGISTER_FAIL);
        }
        Integer staffId = staffService.staffRegister(staffAccount, staffPassword);
        if (staffId == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, STAFF_REGISTER_REPEAT, STAFF_REGISTER_FAIL);
        }
        return ResultUtils.success(staffId, STAFF_REGISTER_SUCCESS);
    }

    @ApiOperation("员工更改")
    @PostMapping("/change")
    public BaseResponse<String> staffChange(@RequestBody StaffChangeRequest staffChangeRequest) {
        Integer staffId = staffChangeRequest.getStaffId();
        String staffAccount = staffChangeRequest.getStaffAccount();
        String staffPassword = staffChangeRequest.getStaffPassword();
        Integer staffPrivilege = staffChangeRequest.getStaffPrivilege();

        String staffAccounted = staffService.staffChange(staffId, staffAccount, staffPassword, staffPrivilege);
        if (staffAccounted == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, "员工失败成功");
        }
        return ResultUtils.success(staffAccounted, "员工修改成功");
    }

    @ApiOperation("获取员工")
    @GetMapping("/search")
    public BaseResponse<List<StaffVo>> staffSearch(String staffAccount) {
        List<StaffVo> staffVoList = staffService.staffSearch(staffAccount);
        return ResultUtils.success(staffVoList, "员工列表");
    }
}